﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using libAddressBook.BUS;
using libDAL;
using Telerik.WinControls.Data;
using Telerik.WinControls.UI;
namespace libAddressBook.FORMS
{
    public partial class frmManageCompanyInfo : Telerik.WinControls.UI.RadForm
    {
        private int intXCoordinate;
        private int intYCoordinate;
        
        private string strCompanyID = string.Empty;
        private string strHistoryID = string.Empty;
        private DataRow drCurrentSelected; //is the selected value from the main window
        private clsBUSInterfaceCompanyHistory _BUS;
        private clsUsersMainBUSInterface _BUSMain;
        private bool bolApply = false;

        public DataRow get_DefaultCompany()
        {
            DataRow drNew = _BUS.get_CompanyHistory(getset_CompanyID).NewRow();
            DataTable dtRows = _BUS.get_CompanyHistory(getset_CompanyID).Clone() ;
            try
            {
               // return drCurrentSelected;
                DataRow[] drGet = { };

                drGet = _BUS.get_CompanyHistory(getset_CompanyID).Select("comDefault = 1");
                if (drGet.Length > 0)
                {
                    for (int intCol = 0; intCol < dtRows.Columns.Count; intCol++)
                    {
                        drNew[dtRows.Columns[intCol].ColumnName] = drGet[0][dtRows.Columns[intCol].ColumnName];
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " get_DefaultCompany");
            }

            return drNew;
        
        }

        public DataRow getset_CompanyCurrentSelected
        {
            
            get
            {
                return drCurrentSelected;
                DataRow[] drGet = { };
                DataRow drReturn;
                drGet =  _BUS.get_CompanyHistory(getset_CompanyID).Select("acccode = 'H'");
                if (drGet.Length > 0)
                {
                    drReturn = _BUS.get_CompanyHistory(getset_CompanyID).NewRow();

                    for (int intCol = 0; intCol < _BUS.get_CompanyHistory(getset_CompanyID).Columns.Count; intCol++)
                    {
                        drReturn[intCol] = drGet[0][intCol];
                    }
                }

                return drReturn;

            }
            set
            {
                drCurrentSelected = value;
            }
        }
        
        public string getset_CompanyID
        {
            get
            {
                return strCompanyID;
            }
            set
            {
                strCompanyID = value;
            }
        }

        public void set_DAL(ref clsBUSInterfaceCompanyHistory _BUSMain)
        {
            this._BUS = _BUSMain; 
        }

        public void set_MainBUS(ref clsUsersMainBUSInterface _BUSLMain)
        {
            this._BUSMain = _BUSLMain;
        }

        public string getset_CompanyHistoryID
        {
            get
            {
                return strHistoryID;
            }
            set
            {
                strHistoryID = value;
            }
        }

        public frmManageCompanyInfo()
        {
            InitializeComponent();
            _BUS = new clsBUSInterfaceCompanyHistory();
        }

        public void set_NewLocation(int intX, int intY)
        {
            intXCoordinate = intX;
            intYCoordinate = intY;
        }

        private void frmManageCompanyInfo_Load(object sender, EventArgs e)
        {
            set_Dataset();
        }

        private void set_Fields()
        {
            try
            {
                    txtID.Text = clsGenFunction.get_checkStringNull(dgvDetails.CurrentRow.Cells["comid"].Value).ToString().Trim();
                    txtRegistrationNo.Text = clsGenFunction.get_checkStringNull(dgvDetails.CurrentRow.Cells["comregno"].Value).ToString().Trim();
                    txtCompanyName.Text = clsGenFunction.get_checkStringNull(dgvDetails.CurrentRow.Cells["comname"].Value).ToString().Trim();
                    txtAddressLine1.Text = clsGenFunction.get_checkStringNull(dgvDetails.CurrentRow.Cells["addline1"].Value).ToString().Trim();
                    txtAddressLine2.Text = clsGenFunction.get_checkStringNull(dgvDetails.CurrentRow.Cells["addline2"].Value).ToString().Trim();
                    txtAddressLine3.Text = clsGenFunction.get_checkStringNull(dgvDetails.CurrentRow.Cells["addline3"].Value).ToString().Trim();
                    txtAddressLine4.Text = clsGenFunction.get_checkStringNull(dgvDetails.CurrentRow.Cells["addline4"].Value).ToString().Trim();
                    txtCityCode.Text = clsGenFunction.get_checkStringNull(dgvDetails.CurrentRow.Cells["citycode"].Value).ToString().Trim();
                    txtCountryCode.Text = clsGenFunction.get_checkStringNull(dgvDetails.CurrentRow.Cells["coucode"].Value).ToString().Trim();
                    txtStateCode.Text = clsGenFunction.get_checkStringNull(dgvDetails.CurrentRow.Cells["stacode"].Value).ToString().Trim();
                    txtAreaCode.Text = clsGenFunction.get_checkStringNull(dgvDetails.CurrentRow.Cells["areacode"].Value).ToString().Trim();
                    txtPotalCode.Text = clsGenFunction.get_checkStringNull(dgvDetails.CurrentRow.Cells["poscode"].Value).ToString().Trim();
                    txtPortID.Text = clsGenFunction.get_checkStringNull(dgvDetails.CurrentRow.Cells["portid"].Value).ToString().Trim();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " set_Fields");
            }
        }

        public bool set_CompanyHistory(string strCompanyID)
        {
            bool bolStatus = false;
            try
            {
                if (_BUS.set_Records(strCompanyID) == true)
                {
                    bolStatus = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " set_CompanyHistory"); 
            }

            return bolStatus;
        }

        private void set_Dataset()
        {
            try
            {
                    dgvDetails.DataSource = _BUS.get_CompanyHistory(getset_CompanyID);
                    
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " set_Dataset"); 
            }
        }

        private void set_ValuesInDatarow()
        {
            try
            {
               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " set_ValuesInDatarow");
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
           // set_ApplyOrOK();
            set_Apply("Ok");
            DialogResult = DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                _BUS.set_RejectChanges();
                DialogResult = DialogResult.Cancel;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " btnCancel_Click"); 
            }
        }

        private void set_CurrentPassedDataRowFromMainForm()
        {
            this.drCurrentSelected["comregno"] = txtRegistrationNo.Text.Trim();
            this.drCurrentSelected["comname"] = txtCompanyName.Text.Trim();
            this.drCurrentSelected["addline1"] = txtAddressLine1.Text.Trim();
            this.drCurrentSelected["addline2"] = txtAddressLine2.Text.Trim();
            this.drCurrentSelected["addline3"] = txtAddressLine3.Text.Trim();
            this.drCurrentSelected["addline4"] = txtAddressLine4.Text.Trim();
            this.drCurrentSelected["citycode"] = txtCityCode.Text.Trim();
            this.drCurrentSelected["coucode"] = txtCountryCode.Text.Trim();
            this.drCurrentSelected["stacode"] = txtStateCode.Text.Trim();
            this.drCurrentSelected["areacode"] = txtAreaCode.Text.Trim();
            this.drCurrentSelected["poscode"] = txtPotalCode.Text.Trim();
            this.drCurrentSelected["portid"] = txtPortID.Text.Trim();

            this.drCurrentSelected["companyHistoryID"] = dgvDetails.CurrentRow.Cells["companyHistoryID"].Value.ToString();
        }

        private DataRow set_AlterSelectedHistory()
        {

            DataRow drInsert = _BUS.get_CompanyAllHistory.NewRow();
            try
            {
                for (int intCol = 0; intCol < dgvDetails.Columns.Count; intCol++)
                {
                    drInsert[intCol] = dgvDetails.CurrentRow.Cells[intCol].Value;
                }
                //alter the specific fields using the entry fields from UI
                drInsert["comregno"] = txtRegistrationNo.Text.Trim();
                drInsert["comname"] = txtCompanyName.Text.Trim();
                drInsert["addline1"] = txtAddressLine1.Text.Trim();
                drInsert["addline2"] = txtAddressLine2.Text.Trim();
                drInsert["addline3"] = txtAddressLine3.Text.Trim();
                drInsert["addline4"] = txtAddressLine4.Text.Trim();
                drInsert["citycode"] = txtCityCode.Text.Trim();
                drInsert["coucode"] = txtCountryCode.Text.Trim();
                drInsert["stacode"] = txtStateCode.Text.Trim();
                drInsert["areacode"] = txtAreaCode.Text.Trim();
                drInsert["poscode"] = txtPotalCode.Text.Trim();
                drInsert["portid"] = txtPortID.Text.Trim();
                drInsert["companyHistoryID"] = clsGenFunction.get_HistoryID(txtID.Text.Trim(), _BUS.get_DAL); 
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " set_AlterSelectedHistory"); 
            }
            return drInsert;

        }

        

        private void set_Apply(string strType)
        {
            try
            {
                    if (rboNew.IsChecked == true)
                    {
                        if (bolApply == false)
                        {
                            //copy the contents of the selected row to the new datarow;
                            DataRow drInsert = set_AlterSelectedHistory();

                            //insert the new record in the dataset of history
                            if (_BUS.AddNewRowHistory(drInsert) == true)
                            {
                                if (chkDefault.Checked == true)
                                {
                                    setDefault(drInsert["companyHistoryID"].ToString());
                                    _BUSMain.set_UpdateAddressBook(drInsert, getset_CompanyHistoryID);
                                }

                                dgvDetails.DataSource = _BUS.get_CompanyHistory(getset_CompanyID);
                                dgvDetails.ClearSelection();
                                bolApply = true;
                                //dgvDetails.Rows.Add(drInsert);
                            }
                        }
                    }
                    else if (rboAlter.IsChecked == true)
                    {
                        if (bolApply == false)
                        {
                            //alter the address details of the selected address from the main window
                            set_CurrentPassedDataRowFromMainForm();

                            if (chkDefault.Checked == true)
                            {
                                this.drCurrentSelected["comDefault"] = true;
                            }

                            //this function alters the fields in the dataset
                            if (_BUS.set_SpecificCopanyDetails(dgvDetails.CurrentRow.Cells["companyHistoryID"].Value.ToString(), drCurrentSelected) == true)
                            {
                                //adjusts the records the default record
                                if (chkDefault.Checked == true)
                                {

                                    setDefault(dgvDetails.CurrentRow.Cells["companyHistoryID"].Value.ToString());
                                    _BUSMain.set_UpdateAddressBook(drCurrentSelected, getset_CompanyID);
                                }
                                // dgvDetails.Rows.Add(drCurrentSelected);
                                if (strType == "Apply")
                                {
                                    dgvDetails.DataSource = _BUS.get_CompanyHistory(getset_CompanyID);
                                    dgvDetails.ClearSelection();
                                }
                            }
                            bolApply = true;
                        }
                    }  
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " btnApply_Click");
            }
        }

        private void set_ClearSelectedRowsAndCells()
        {
            dgvDetails.ClearSelection();
            this.dgvDetails.CurrentRow = null;
            this.dgvDetails.TableElement.Update(GridUINotifyAction.StateChanged);
           

        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            set_Apply("Apply");
            set_ClearSelectedRowsAndCells();
        }

        private void dgvDetails_CellClick(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e)
        {
            bolApply = false;
            string strx = dgvDetails.CurrentRow.Cells["companyHistoryID"].Value.ToString(); 
            set_Fields();
        }

        private void cbbRefresh_Click(object sender, EventArgs e)
        {
            clearSetup();
        }

        private void cbbNew_Click(object sender, EventArgs e)
        {
            clearSetup();
        }

        private void clearSetup()
        {
            txtID.Text = string.Empty;
            txtRegistrationNo.Text = string.Empty;
            txtCompanyName.Text = string.Empty;
            txtAddressLine1.Text = string.Empty;
            txtAddressLine2.Text = string.Empty;
            txtAddressLine3.Text = string.Empty;
            txtAddressLine4.Text = string.Empty;
            txtCityCode.Text = string.Empty;
            txtCountryCode.Text = string.Empty;
            txtStateCode.Text = string.Empty;
            txtAreaCode.Text = string.Empty;
            txtPotalCode.Text = string.Empty;
            txtPortID.Text = string.Empty;
            dgvDetails.ClearSelection();
        }

        private void setDefault(string strCompanyHistoryID)
        {
            if (chkDefault.Checked == true)
            {
                _BUS.set_Default(strCompanyHistoryID, 1);
                //dgvDetails.DataSource = _BUS.get_CompanyHistory;
            }
            else if( chkDefault.Checked == false)
            {
                _BUS.set_Default(strCompanyHistoryID, 0);
              //  dgvDetails.DataSource = _BUS.get_CompanyHistory;
            }
        }

        private void chkDefault_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
              //  setDefault();
              
            }
            catch (Exception ex)
            { 
                
            }
        }
    }
}
